package model.type;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map.Entry;

import db.SubjectAttributeTypeDAO;

public class SubjectAttributeType {
	private static HashMap<Integer, SubjectAttributeType> map 
		= new HashMap<Integer, SubjectAttributeType>();
	
	private int id;
	private int subjectType;
	private String name;
	private int dataType;
	private int orderBy;
	private boolean required;
	private boolean multiple;
	private boolean createdByDefault;
	
	// LOAD EVERYTHING INTO HASHMAP FOR QUICK REFERENCE IN THE FUTURE
	static{		
		for(SubjectAttributeType t: SubjectAttributeTypeDAO.getAll()){
			map.put(t.getId(), t);
		}		
	}
	
	public SubjectAttributeType(int id, int type, String name, int dataType,
			int orderBy, String required, String multiple, String createdByDefault) {
		this.id = id;
		this.subjectType = type;
		this.name = name;
		this.dataType = dataType;
		this.orderBy = orderBy;
		this.required = "Y".equalsIgnoreCase(required);
		this.multiple = "Y".equalsIgnoreCase(multiple);
		this.createdByDefault = "Y".equalsIgnoreCase(createdByDefault);
	}
	
	public static SubjectAttributeType getSubjectAttributeType(int id){
		return map.get(id);
	}
	
	public int getId() {
		return id;
	}

	public int getSubjectType() {
		return subjectType;
	}
	
	public String getName() {
		return name;
	}
	public int getDataType() {
		return dataType;
	}

	public int getOrderBy() {
		return orderBy;
	}

	public boolean isRequired() {
		return required;
	}

	public boolean isMultiple() {
		return multiple;
	}

	public boolean isCreatedByDefault() {
		return createdByDefault;
	}
	
	public static ArrayList<SubjectAttributeType> getAll(){
		ArrayList<SubjectAttributeType> list = new ArrayList<SubjectAttributeType>();
		for(Entry<Integer, SubjectAttributeType> e: map.entrySet()){
			list.add(e.getValue());
		}
		return list;
	}
	
	public static ArrayList<SubjectAttributeType> getAll(Integer ... subjectType){
		ArrayList<Integer> subjectTypes = new ArrayList<Integer>();
		subjectTypes.addAll(Arrays.asList(subjectType));		
		ArrayList<SubjectAttributeType> list = new ArrayList<SubjectAttributeType>();
		for(Entry<Integer, SubjectAttributeType> e: map.entrySet()){			
			if(subjectTypes.contains((Integer)e.getValue().getSubjectType()))list.add(e.getValue());
		}
		return list;
	}
}
